A value is added or deleted to a set. I want to get the smallest value.
A value is added or deleted to a set. I want to get the smallest value.
→ Prepare a heapq that contains the deleted values and skip reading it when acquiring.
code:python
from heapq import *
added = []
removed = []
# add
heappush(added, 1)
heappush(added, 2)
# remove
heappush(removed, 1)
# get top
while removed and added0 == removed0: heappop(added)
heappop(removed)
---
This page is auto-translated from /nishio/ある集合に値が追加削除される。最小の値を取得したい。. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.